package com.yyw.bean;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class Employee implements Serializable , UserDetails {

    private static final long serialVersionUID = 1L;

    //@JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    private String username;

    private String name;

    private String password;

    private String phone;

    private String sex;

    private String idNumber;

    private Integer status;

    //下面4个属性属于公共字段。也就是很多表都有这4个列，很多类里面都有这4个属性
    // 我们希望mybatisplus 在执行表的添加或者更新操作的时候，自动的给这4个属性赋值或者给其中的某些属性赋值。
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime; //添加时赋值

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime; //添加和更新时赋值

    @TableField(fill = FieldFill.INSERT)
    private Long createUser; //添加时赋值

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser; //添加和更新时赋值

    //表示这个员工属于什么角色
    @TableField(exist = false)
    private List<Role> roleList;

    //账号的角色
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> list = new ArrayList<SimpleGrantedAuthority>();
        for (Role role : roleList) {
            list.add(new SimpleGrantedAuthority(role.getKeyword()));
        }
        return list;
    }

    //账号是否过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //账号是否没有锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //认证信息是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //表示员工账号是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
